<?php
// ----------------------------------------------------------------------
// pn-commerce Shopping Cart Solution for PostNuke
// http://pncommerce.de
// http://www.sourceforge.net/projects/pn-commerce/
// ----------------------------------------------------------------------
// Source file:  $Source: /cvsroot/pn-commerce/pn-commerce/pnsec.inc.php,v $
//
// Last changes from:  $Author: landseer $
//
// Revision:           $Revision: 1.6 $
//
// Date of revision    $Date: 2004/09/12 11:17:13 $
//
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
//-----------------------------------------------------------------------

require_once("modules/pncommerce/common.php");

/**
 * pnVarCheckFromInput
 * read parameters send in the url via pnVarCleanFromInput and checks if a given condition is met
 * Parameters can be an array ( 'name', 'condition' ) or a string 'name_of_calling_function'.
 * In this case we read the names and conditions from the corresponding file in the /pnsec 
 * folder. 
 * This function returns immediately when the first error occurs.
 *@params array( 'name', 'condition' ) or
 *@params string function name
 *@returns array( 'values', 'errormsg' )
 *@return value array of values read
 *@return errormsg string with errormsg or empty if no error occurs
 */       
function pnVarCheckFromInput()
{
    $resarray = array();
    foreach (func_get_args() as $params)
    {
        if( is_array( $params ) )
        {
            // the varpair is an array of name, condition
            // 0 = variable name
            // 1 = condition to check
            $name = $params[0]; 
            $ourvar = pnVarCleanFromInput( $name );
            $error = false;
            if( !empty($ourvar) )
            {
                $conditions = explode( ",", $params[1] );
                $nostriptags = false;
                foreach( $conditions as $cond )
                {
                    switch( $cond )
                    {
                        case 'number':
                            if( is_numeric( $ourvar ) )
                            {
                                break;
                            }
                            $error = true;
                            break;
                        case 'string':
                            if( is_string( $ourvar ) )
                            {
                                break;
                            }
                            $error = true;
                            break;
                        case 'boolean':
                            if( is_bool( $ourvar ) )
                            {
                                break;
                            }
                            $error = true;
                            break;
                        case 'array':
                            if( is_array( $ourvar ) )
                            {
                                break;
                            }
                            $error = true;
                            break;
                        case 'notempty':
                            if( !empty( $ourvar ) )
                            {
                                break;
                            }
                            $error = true;
                            break;
                        case 'noblank':
                            if( strstr( $ourvar, " " ) == false )
                            {
                                break;
                            }
                            $error = true;
                            break;
                        case 'uppercase':
                            if( $ourvar == strtoupper( $ourvar ) )
                            {
                                break;
                            }
                            $error = true;
                            break;
                        case 'lowercase':
                            if( $ourvar == strtolower( $ourvar ) )
                            {
                                break;
                            }
                            $error = true;
                            break;
                        case 'authkey':
                            if( pnModGetVar( 'pncommerce', 'UseAuthKeys' ) == 'on' )
                            {
                                if( pnSecConfirmAuthKey( $ourvar ) )
                                {
                                    break;
                                }
                                $error = true;
                            }
                            break;
                        case 'inc':
                            $ourvar = intval($ourvar) + 1;
                            break;
                        case 'dec':
                            $ourvar = intval($ourvar) - 1;
                            break;
                        case 'removecr':
                            $ourvar = preg_replace( "!(\r\n)|(\r)|(\n)!", "", $ourvar );
                            break;
                        case'nostriptags':
                            $nostriptags = true;
                            break;
                        default:
                            if( !preg_match( $cond, $ourvar ) )
                            {
                                $error = true;
                            }
                    }                            
                    if( $error === true )
                    {
                        $errormsg = sprintf( pnVarPrepForDisplay( _PNC_INVALIDARGUMENT ), $name );
                        return array( 'values'=>array(), 'errormsg'=>showerrorpage($errormsg . " ($ourvar)", __FILE__, __LINE__ ) );
                    }
                }  // foreach
                if( $nostriptags == false )
                {
                    $ourvar = strip_tags( $ourvar );
                }
            } //if isset($ourvar)
//echo "<br>$name = $ourvar <br>";
            // Add to result array
            // array_push($resarray, $ourvar);
            $resarray[$name] = $ourvar;
//print_r($resarray);echo "<br>";
        }
        elseif( is_string( $params ) )
        {
            // varpair is not an array but an string. In this case its the name of the
            // calling function
            // we now have to include the corresponding file from the pnsec folder with name
            // and condition settings
            include_once( 'modules/pncommerce/pnsec/'.$params.'.php' );
            if( !is_array( $pnSecArray ) )
            {
                return array( 'values'=> array(), 'errormsg' => sprintf( pnVarPrepForDisplay( _PNC_WRONGPARAMETERINEXTERNALPNSECFILE ), $params ) );
            }
            $resarray = array();
            foreach( $pnSecArray as $pnSecEntry )
            {
                // pass this array of name and condition recursively to this function

                $res = pnVarCheckFromInput( $pnSecEntry );
                if( !empty( $res['errormsg'] ) )
                {
                    return $res;
                }
                else
                {
                    $keys = array_keys( $res['values'] );
                    //array_push( $finalresarray, $res['values'][0] );
                    $resarray[$keys[0]] = $res['values'][$keys[0]];
                }
            }
            return array( 'values' => $resarray, 'errormsg' => false );
        }
        else
        {
            // neither array nor string - return an error 
            return array( 'values'=> array(), 'errormsg' => pnVarPrepForDisplay( _PNC_WRONGPARAMETERFORPNVARCHECK ) );
        }         
    }
    // Return vars
//echo "<br>start:<pre>";
//print_r($resarray);
//echo "</pre>:ende<br>";
    return array( 'values' => $resarray, 'errormsg' => false );
}

?>